//
//  XMFeedbackViewController.m
//  XunMi
//
//  Created by MacBook on 2024/3/4.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMFeedbackListController.h"
#import "XMFeedbackTableViewCell.h"
#import "XMFeedbackViewController.h"
#import "XMFeedbackModel.h"
#import "XMFeedbackDetailViewController.h"

@interface XMFeedbackListController ()
@property (nonatomic, strong) NSMutableArray <XMFeedbackModel *> *dataArray;
@property (nonatomic, strong) NSMutableArray *tagsArray;
@property (nonatomic, assign) NSInteger page;
@end

@implementation XMFeedbackListController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initVariables];
    [self setupSubviews];
    [self feedbackList];
    [self setupRefresh];
}


#pragma mark - refresh
- (void)setupRefresh {
    XMWeakSelf(self);
    self.tableView.mj_header = [XMRefreshNormalHeader headerWithRefreshingBlock:^{
        XMStrongSelf(self);
        [self feedbackListRefresh];
    }];
    self.tableView.mj_footer = [XMRefreshBackFooter footerWithRefreshingBlock:^{
        XMStrongSelf(self);
        self.page += 1;
        [self feedbackList];
    }];
    self.tableView.mj_footer.hidden = YES;
}

- (void)feedbackListRefresh {
    self.page = 1;
    [self.dataArray removeAllObjects];
    [self feedbackList];
}

#pragma mark - feedbackList
- (void)feedbackList {
    [MBProgressHUD showHUDActivityMsg:nil toView:kWindow];
    NSMutableDictionary *dic = @{
        @"page_no":@(self.page),
        @"page_size":@(10)
    }.mutableCopy;
    [YHNetworkManager.sharedInstance postWithUrl:@"api/report/feedback/obtain" parameters:dic completeBlock:^(XMResponseTip * _Nonnull tip, id  _Nullable result) {
        [self.tableView hideStatusView];
        if (tip.success) {
            [MBProgressHUD hideHUD];
            NSArray *record = [result safe_objectForKey:@"record"];
            NSArray *array = [XMFeedbackModel mj_objectArrayWithKeyValuesArray:record];
            [self.dataArray addObjectsFromArray:array];
            if (self.dataArray.count == 0) {
                [self.tableView showStatusView:(YHViewStatusTypeEmptyData)];
            }
            self.tagsArray = [NSMutableArray arrayWithArray:[result safe_objectForKey:@"types"]];
        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        self.tableView.mj_footer.hidden = self.dataArray.count == 0;
        if (tip.loadMore) {
            [self.tableView.mj_footer endRefreshing];
        } else {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    }];
}

#pragma mark - 初始化变量
- (void)initVariables {
    self.page = 1;
    [self tableViewRegisterNibWithNibName:kStringFromClass(XMFeedbackTableViewCell)];
}
#pragma mark - setupSubViews
- (void)setupSubviews {
    [self.navLeft2Btn setTitle:@"意见反馈" forState:UIControlStateNormal];
    [self.navRightBtn setTitle:@"去反馈" forState:UIControlStateNormal];
    [self.navRightBtn setBackgroundColor:kAppMainColor];
    self.navRightBtn.titleLabel.font = kUIFont(14);
    [self.navRightBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    
    [self.navRightBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(72, 32));
    }];
    kCornerRadius(self.navRightBtn, 16, 0, UIColor.clearColor);
    [self.navRightBtn addTarget:self action:@selector(feedback) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - Actions
- (void)feedback {
    XMFeedbackViewController *vc = [[XMFeedbackViewController alloc] initWithNibName:kStringFromClass(XMFeedbackViewController) bundle:nil];
    vc.tagsArray = self.tagsArray;
    [self.navigationController pushViewController:vc
                                         animated:YES];
}

#pragma mark - UITableViewDataSource & UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 88;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XMFeedbackTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kStringFromClass(XMFeedbackTableViewCell)];
    cell.model = [self.dataArray safe_objectAtIndex:indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    XMFeedbackModel *model = [self.dataArray safe_objectAtIndex:indexPath.row];
    XMFeedbackDetailViewController *vc = [[XMFeedbackDetailViewController alloc] initWithNibName:kStringFromClass(XMFeedbackDetailViewController) bundle:nil];
    vc.model = model;
    [self.navigationController pushViewController:vc
                                         animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}

- (NSMutableArray<XMFeedbackModel *> *)dataArray {
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

@end
